package com.ibm.epss.android;

import android.os.Handler;
import android.os.Message;
import com.ibm.micro.client.mqttv3.*;

public class PkgExpCallBack implements MqttCallback {
	private String instanceData;
	private Handler handlerUI;
	public String strLog = "";

	public PkgExpCallBack(String instance, Handler updateUIHandler) {
		this.instanceData = instance;
		this.handlerUI = updateUIHandler;
	}

	public void messageArrived(MqttTopic topic, MqttMessage message) {
		try {
			strLog += message.toString();
			Message msg = new Message();
			msg.obj = message.toString();
			handlerUI.sendMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void connectionLost(Throwable cause) {
		System.out.println("Connection lost on instance \"" + instanceData
				+ "\" with cause \"" + cause.getMessage() + "\" Reason code "
				+ ((MqttException) cause).getReasonCode() + "\" Cause \""
				+ ((MqttException) cause).getCause() + "\"");
		/*try {
			while (0 != MQTTUtil.Connect()) {
				Thread.sleep(3000);
			}
		} catch (InterruptedException e) {
		}*/
		
	}

	public void deliveryComplete(MqttDeliveryToken token) {
		try {
			System.out.println("Delivery token \"" + token.hashCode()
					+ "\" received by instance \"" + instanceData + "\"");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}